//! WhirliGigAdmin.debug.js
//

(function() {

Type.registerNamespace('WhirliGigAdmin');

////////////////////////////////////////////////////////////////////////////////
// GigAdmin

GigAdmin = function GigAdmin() {
    /// <field name="_publishedHomeExists" type="Boolean" static="true">
    /// </field>
}
GigAdmin.Page = function GigAdmin$Page() {
    $('#Account li').first().html(ThemeYoshi.email);
    $('#SpaceAdmin').html(Strings.paraCase(ThemeBase.get_baseName()) + ' Space Commander');
    $('a.UserLink:contains(Publish pages)').attr('title', 'publish pages for public viewing');
    var goToPages = $('a.UserLink:contains(Go to published pages)').disableLink(true, 'no pages are currently published');
    new Await().addAw(FileBlobBase.existsAw, Uri.join('../pages', ThemeYoshi.defaultPageName + '.html')).addDx(function(aw) {
        GigAdmin._publishedHomeExists = aw.get_result();
    }).addDx(function() {
        if (GigAdmin._publishedHomeExists) {
            goToPages.disableLink(false, 'go to published home page');
        }
    }).commit();
}
GigAdmin.PublishPages = function GigAdmin$PublishPages() {
    new Await().loadScript(Uri.scripts('Scripts/WhirliGigEdit.js')).addDx(function() {
        Publish.PublishEv(null);
    }).commit();
}
GigAdmin.ToPublished = function GigAdmin$ToPublished() {
    if (!GigAdmin._publishedHomeExists) {
        Ask.ok('No files are published.');
        return;
    }
    Nsb.Storage.setLocal(ThemeYoshi.get_curatingKey(), false);
    window.location.href = Uri.join('pages', ThemeYoshi.defaultPageName + '.html');
}
GigAdmin.ChangeEmail = function GigAdmin$ChangeEmail() {
    new Await().addAw(ss.Delegate.create(new Ask(), new Ask().textAw), { Msg: 'New email address', Verify: function(v) {
        var vv = (v).trim();
        if (!Rx.emailFilter.test(vv)) {
            Ask.ok('please supply a valid email address');
            return false;
        }
        return true;
    } }).addAw(DomElement.requireResultAw, DomElement.notFailOrEmpty).addDx(function(aw) {
        aw.set_item('newEmail', (aw.get_result()).trim());
    }).addAw(GigAdmin._changeAccountInfoAw).commit();
}
GigAdmin.ChangePassword = function GigAdmin$ChangePassword() {
    var html = $("<div class='ChangeAccount'/>");
    var password1 = $("<input type='password'/>").appendTo(html).before('current<br>');
    var password2 = $("<input type='password'/>").appendTo(html).before('<br>new<br>');
    var password3 = $("<input type='password'/>").appendTo(html).before('<br>new again<br>');
    new Await().addAw(ss.Delegate.create(new Ask(), new Ask().okCancelAw), { Msg: 'Change curating password', Html: html, Verify: function() {
        if (password1.val() !== Nsb.Storage.getLocal(ThemeYoshi.get_curatePasswordKey())) {
            Ask.ok('incorrect password');
            return false;
        }
        if (String.isNullOrEmpty(password2.val().trim())) {
            Ask.ok('please supply a new password');
            return false;
        }
        if (password2.val() !== password3.val()) {
            Ask.ok("new passwords don't match");
            return false;
        }
        if (password2.val().length < 8) {
            Ask.ok('new password must be at least 8 characters');
            return false;
        }
        return true;
    } }).addAw(DomElement.requireResultAw, DomElement.notFail).addDx(function(aw) {
        aw.set_item('newPassword', password2.val());
    }).addAw(GigAdmin._changeAccountInfoAw).addDx(function(aw) {
        Nsb.Storage.setLocal(ThemeYoshi.get_curatePasswordKey(), aw.get_item('newPassword'));
    }).commit();
}
GigAdmin._changeAccountInfoAw = function GigAdmin$_changeAccountInfoAw(awp) {
    /// <param name="awp" type="Await">
    /// </param>
    Cluster.spinner(true, 0.35);
    var args = {};
    args['password'] = Nsb.Storage.getLocal(ThemeYoshi.get_curatePasswordKey());
    if (awp.get_item('newPassword') != null) {
        args['newPassword'] = awp.get_item('newPassword');
    }
    if (awp.get_item('newEmail') != null) {
        args['newEmail'] = awp.get_item('newEmail');
    }
    var options = {};
    options.url = Uri.app('account.php');
    options.dataType = 'json';
    options.data = args;
    options.type = 'GET';
    options.success = function(data, textStatus, request1) {
        Cluster.spinner(false);
        var pr = data;
        try {
            if (pr.result) {
                ThemeYoshi.reloadPage();
            }
        }
        catch (e) {
            options.error(request1, 'account update failed', e);
            return;
        }
        awp.done();
    };
    options.error = function(request, textStatus, error) {
        Cluster.spinner(false);
        Inform.error(String.format('Error updating account {0} {1}', textStatus, error));
        awp.handle(new Error(Exceptions.blobLoadError));
    };
    $.ajax(options);
}


GigAdmin.registerClass('GigAdmin');
GigAdmin._publishedHomeExists = false;
})();

//! This script was generated using Script# v0.7.4.0
